package cn.edu.ctbu.datajpa01.controller;

import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class StudentController {

    @Autowired
    StudentService studentService;

    @RequestMapping("/student/list")
    public String list(HttpServletRequest httprequest,Model model){

        HttpSession session=httprequest.getSession();

        if(session.getAttribute("userid")==null){

            return "redirect:/login/user";
        }

        List<Student> students = studentService.findAll();

        model.addAttribute("data", students);

        return "/student/list";

    }

    @GetMapping("/studentFind")
    @ResponseBody
    public Student findByName(String name){

        Student byname = studentService.findByName(name);
        return byname;
    }


}